<?php
	error_reporting(0);
	require_once "data/handler.php";
	require_once "data/cartfunctions.php";
	
	session_start();	
	if(!isset($_SESSION["isLoggedIn"]) || !($_SESSION["isLoggedIn"] === true)){
        $songArray = array();
		$_SESSION['songs'] = $songArray;
    }
	
	
	
	function viewCart(){
		$total = 0;

		if(count($_SESSION['songs'])>0){
			echo '<table><tr><th >Album Name</th><th>Track No.</th><th>Title</th><th>Duration</th><th>Price</th>';
			$prodpath = "data/products.xml";
			$prods = simplexml_load_file($GLOBALS['prodpath']);
			
			for($j=0;$j<count($_SESSION['songs']);$j++){
					$song = getAlbum($_SESSION['songs'][$j]->getAlbumID());
					$a = (int) $_SESSION['songs'][$j]->getAlbumID();
					
						echo    '<tr>';
						for($i = 0; $i < count($song); $i++){
							if($_SESSION['songs'][$j]->getTrackNo()==($i+1)){
								echo    '<td align="center">'.$prods->album[$a-1]->albumtitle.'</td>';
								echo    '<td align="center">'.$song[$i]["trackno"].'</td>';
								echo    '<td align="center">'.$song[$i]["title"].'</td>';
								echo    '<td align="center">'.$song[$i]["duration"].'</td>';
								echo    '<td align="center">$'.$song[$i]["price"].'</td>';
								echo    '<td><audio controls="controls"><source src="'.$song[$i]["prev"].'" type="audio/mpeg"/></audio></td>';							
								echo	'<td ><a href="addtocart.php?action=remove&trackno='.($i+1).'&albumid='.$a.'">'."Remove from Cart".'</a></td>';
								$total = $total+floatval($song[$i]["price"]);
							}
						}
						echo    '</tr>';
					
			}
			echo '<tr><td>Total: $'.$total.'</td>';
			echo '<td><a href="addtocart.php?action=empty&trackno=0&albumid=0">Empty Cart</td></tr>';
			echo '<td><a href="addtocart.php?action=checkout&trackno=0&albumid=0">Checkout</td></tr>';
			echo '</table>';
		}
		else echo 'Cart is empty.';
	}
	
	function randomCart(){
		$total = 0;
		$songArray = array();
		$prods1 = simplexml_load_file($GLOBALS['prodpath']);
		
		for($j=0;$j<5;$j++){
			$rand2 = rand(1,count($prods1->children()));
			$rand1 = rand(1,count($prods1->album[$rand2-1]->songs->children()));
			$song = new cartSong($rand2,$rand1);
			$songArray[] = $song;
		}
		
		if(count($songArray>0)){
			echo '<table><tr><th >Album Name</th><th>Track No.</th><th>Title</th><th>Duration</th><th>Price</th>';
			$prodpath = "data/products.xml";
			$prods = simplexml_load_file($GLOBALS['prodpath']);
			
			for($j=0;$j<count($songArray);$j++){
					$song = getAlbum($songArray[$j]->getAlbumID());
					$a = (int) $songArray[$j]->getAlbumID();
					
						echo    '<tr>';
						for($i = 0; $i < count($song); $i++){
							if($songArray[$j]->getTrackNo()==($i+1)){
								echo    '<td align="center">'.$prods->album[$a-1]->albumtitle.'</td>';
								echo    '<td align="center">'.$song[$i]["trackno"].'</td>';
								echo    '<td align="center">'.$song[$i]["title"].'</td>';
								echo    '<td align="center">'.$song[$i]["duration"].'</td>';
								echo    '<td align="center">$'.$song[$i]["price"].'</td>';
								echo    '<td><audio controls="controls"><source src="'.$song[$i]["prev"].'" type="audio/mpeg"/></audio></td>';							
								if(!(!isset($_SESSION["isLoggedIn"]) || !($_SESSION["isLoggedIn"] === true))) echo	'<td><a href="addtocart.php?action=add&trackno='.($i+1).'&albumid='.$songArray[$j]->getAlbumID().'"><img src="buttons/addtocart.png"/></a></td></tr>';
								
							}
						}
						echo    '</tr>';
					
			}
			//echo '<tr><td>Total: $'.$total.'</td>';
			//echo '<td><a href="addtocart.php?action=empty&trackno=0&albumid=0">Empty Cart</td></tr>';
			//echo '<td><a href="addtocart.php?action=checkout&trackno=0&albumid=0">Checkout</td></tr>';
			echo '</table>';
		}
		else echo 'Cart is empty.';
	}
?>